'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 英雄
 * @author sundbcn@gmail.com (sun debing)
 */

var dataApi = require('../libs/dataApi');
var cacheUtil = require('../util/cacheUtil.js');

exports.getHeroTypeConf = function (hero_type) {
    return dataApi.hero_type.findById(hero_type);
};

/**
 * 获取英雄等级配置
 * @param  {[type]} hero_type    [description]
 * @param  {[type]} hero_lv      [description]
 * @return {[type]}              [description]
 */
exports.getHeroLvConf = function (hero_lv) {
    return _getHeroLvConf(hero_lv);
};

/**
 * 获取英雄等级好友数量上限
 * @param  {[type]} hero_lv      [description]
 * @return {[type]}              [description]
 */
exports.getFriendLimitNum = function (hero_lv) {
    var hero_lv_conf = _getHeroLvConf(hero_lv);
    return hero_lv_conf.friend_num;
};

/**
 * 获取英雄属性等级配置
 * @param  {[type]} hero_lv      [description]
 * @return {[type]}              [description]
 */
exports.getHeroAttrLvConf = cacheUtil.memorize(function (hero_type, hero_lv) {
    var hero_attr_lv_all_conf = dataApi.hero_attribute_lvl.findById(hero_type);
    if (!hero_attr_lv_all_conf) {
        console.error('hero type can not found:' + hero_type);
        return null;
    }
    for (var i = 0; i < hero_attr_lv_all_conf.length; i++) {
        var hero_lv_conf = hero_attr_lv_all_conf[i];
        if (hero_lv_conf.level == hero_lv) {
            return hero_lv_conf;
        }
    }
    console.error('hero lv can not found:' + hero_lv);
    return null;
});

/**
 * 获取英雄的掠夺经验
 * @param  {[type]} hero_lv [description]
 * @return {[type]}         [description]
 */
exports.getHeroPvpExp = function (hero_lv) {
    var hero_lv_conf = _getHeroLvConf(hero_lv);
    if (!hero_lv_conf) {
        console.error('hero lv conf error:' + hero_lv);
        return 0;
    }
    return hero_lv_conf.pvp_exp;
};

/**
 * 获取英雄行动力消耗获得的经验
 * @param  {[type]} hero_lv [description]
 * @return {[type]}         [description]
 */
exports.getHeroApExp = function (hero_lv) {
    var hero_lv_conf = _getHeroLvConf(hero_lv);
    if (!hero_lv_conf) {
        console.error('hero lv conf error:' + hero_lv);
        return 0;
    }
    return hero_lv_conf.pvp_ap_exp;
};

/**
 * 获取英雄等级配置
 * @param  {[type]} hero_lv [description]
 * @return {[type]}         [description]
 */
var _getHeroLvConf = function (hero_lv) {
    var hero_lv_conf = dataApi.hero_lvl.findById(hero_lv);
    if (!hero_lv_conf) {
        console.error('hero lv can not found:' + hero_lv);
        return null;
    }
    return hero_lv_conf;
};
